package sz.szsmk.citizencard.ui.newversion.fragment.presenter

import android.util.Log
import com.google.gson.Gson
import com.youzan.spiderman.utils.MD5Utils
import sz.szsmk.citizencard.DaggerApplication
import sz.szsmk.citizencard.model.network.bean.request.*
import sz.szsmk.citizencard.model.network.bean.response.*
import sz.szsmk.citizencard.mvp.dzsmkpresenter.AppBasePresenter
import sz.szsmk.citizencard.retrofitrxjava.ErrorAction
import sz.szsmk.citizencard.retrofitrxjava.SubscribeResult
import sz.szsmk.citizencard.retrofitrxjava.newversion.ApiCallback
import sz.szsmk.citizencard.retrofitrxjava.newversion.HttpDateSource
import sz.szsmk.citizencard.smkretrofit.NetworkDateSource
import sz.szsmk.citizencard.ui.newversion.fragment.view.IdCodeMvpView
import sz.szsmk.citizencard.util.ACache
import sz.szsmk.citizencard.util.AndroidUtils
import sz.szsmk.citizencard.util.StringUtils
import javax.inject.Inject

/**
 *  author : hyt
 *  date : 2020/10/14
 *  description :
 */
class IdCodePresenter@Inject constructor(var httpDateSourceNotUse: NetworkDateSource)
    : AppBasePresenter<IdCodeMvpView>() {

    //获取等级
    fun getLevel(){
        val baseRequest = IBaseRequest()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        baseRequest.access_token = access_token
        val androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        baseRequest.device_id = AndroidUtils.getAndroidId(DaggerApplication.getIns())
        val refresh_token = ACache.get(DaggerApplication.getIns()).getAsString("refresh_token")
        val key = "access_token=$access_token&device_id=$androidId&key=$refresh_token"
        val stringMd5 = MD5Utils.getStringMd5(key).toUpperCase()
        baseRequest.sign = stringMd5

        addSubscription(HttpDateSource.createApi()?.firstSearchUserLevel(baseRequest), object : ApiCallback<FirstSearchUserLevelRes?> {
            override fun onSuccess(response: FirstSearchUserLevelRes?) {
                response?.let {
                    if (!StringUtils.isBlank(response.data.recognitionLevel.l3) ||
                            !StringUtils.isBlank(response.data.recognitionLevel.l2)) {
                        //是L2 认证
                        //请求二维码
                        getQrNumber()
                        //请求下面数据
                        validCardAuthorization()
                    } else if (!StringUtils.isBlank(response.data.recognitionLevel.l1)) {
                        //提示L2认证
                        mvpView.lessL2()
                    }
                }
            }

            override fun onFailure(model: BaseBean?, e: Throwable?) {
                model?.let { mvpView.onFail(it) }
            }
        })
    }

    //根据token查询 各种卡的状态
    fun validCardAuthorization(){
        val baseRequest = IBaseRequest()
        baseRequest.access_token =  ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        addSubscription(HttpDateSource.createApi()?.validCardAuthorization(baseRequest),
                object : ApiCallback<ValidCardAuthorizationRes?> {
                    override fun onSuccess(response: ValidCardAuthorizationRes?) {
                        response?.let {
                            mvpView.validCardAuthorizationSucc(it)
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                })
    }

    //将卡片授权给用户
    fun cardnoAuthorization(card: String?) {
        val cardnoAuthorizationReq = CardnoAuthorizationReq()
        cardnoAuthorizationReq.card_no = card
        cardnoAuthorizationReq.type = "1"
        val json = Gson().toJson(cardnoAuthorizationReq)
        addSubscription(HttpDateSource.createApi()?.cardnoAuthorization(cardnoAuthorizationReq),
                object : ApiCallback<CardnoAuthorizationRes?> {
                    override fun onSuccess(response: CardnoAuthorizationRes?) {
                        response?.let {
                            getQrNumber()
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                }
        )
    }

    //查询休闲年卡
    fun quryLeisureCards() {
        val queryAllCardsReq = QueryAllCardsReq()
        val access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        queryAllCardsReq.access_token = access_token

        addSubscription(HttpDateSource.createApi()?.quryLeisureCards(queryAllCardsReq),
                object : ApiCallback<QuerLeisureCardsInfoRes?> {
                    override fun onSuccess(response: QuerLeisureCardsInfoRes?) {
                        response?.let {
                            mvpView.quryLeisureCardsSuccess(it)
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                })
    }

    //上传头像
    fun upLoadPic(cardNo: String, photo: String){
        val uploadPic = UploadPic()
        uploadPic.setCardNo(cardNo)
        uploadPic.setPhoto(photo)

        addSubscription(HttpDateSource.createApi()?.upLoadPic(uploadPic),
                object : ApiCallback<BaseBean?> {
                    override fun onSuccess(response: BaseBean?) {
                        response?.let {
                            mvpView.getUploadPic()
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                })
    }

    //获取二维码
    fun getQrNumber(){
        val qrCodeRequest = QRCodeRequest()
        qrCodeRequest.access_token = ACache.get(DaggerApplication.getIns()).getAsString("access_token")
        qrCodeRequest.is_fresh = "1"

        addSubscription(HttpDateSource.createApi()?.getUserQRCode(qrCodeRequest),
                object : ApiCallback<QRCodeBean?> {
                    override fun onSuccess(response: QRCodeBean?) {
                        response?.let {
                            mvpView.getQRCodeSuccess(it)
                        }
                    }

                    override fun onFailure(model: BaseBean?, e: Throwable?) {
                        model?.let { mvpView.onFail(it) }
                    }
                })
    }

}